using System.Collections.Generic;
using System;

namespace IQIGame.Onigao.Framework
{
    /// <summary>
    /// 数组池，只能获取固定长度的数组，用于部分数学计算逻辑
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class ArrayPoolImpl<T>
    {
        private readonly Stack<T[]> pool = new Stack<T[]>();
        private int _arrLength;
        private int _poolSize;

        public ArrayPoolImpl(int arrLength, int poolSize = 20)
        {
            this._arrLength = arrLength;
            this._poolSize = poolSize;
        }

        public int PoolSize
        {
            get => _poolSize;
            set
            {
                if (value < 0) throw new ArgumentException();
                _poolSize = value;
            }
        }

        public T[] Get()
        {
            if (pool.Count > 0)
            {
                return pool.Pop();
            }
            T[] list = new T[_arrLength];
            return list;
        }

        public void Put(T[] list)
        {
            if (list == null)
            {
                LogGame.LogError("ListPool Put list is null");
                return;
            }

            Array.Clear(list, 0, _arrLength);
            if (pool.Count >= _poolSize)
                return;
            pool.Push(list);
        }

        public void Clear()
        {
            pool.Clear();
        }
    }
}
